import store from "@/store";
import axios from "axios";
import { Message } from "element-ui";
import { getTimeStamp } from "@/utils/auth";
import router from "@/router";
// 创建一个axios实例
const TimeOut = 3600;
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 5000,
});
// 请求拦截器
service.interceptors.request.use(
  (config) => {
    if (store.getters.token) {
      if (IsCheckTimeOut()) {
        store.dispatch("user/logout");
        router.push("/login");
        return Promise.reject(new Error("token超时了"));
      }
      config.headers["Authorization"] = `Bearer ${store.getters.token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
function IsCheckTimeOut() {
  let currentTime = Date.now();
  let timeStamp = getTimeStamp();
  return (currentTime - timeStamp) / 1000 > TimeOut;
}
// 响应拦截器

service.interceptors.response.use(
  (response) => {
    const { success, message, data } = response.data;
    if (success) {
      return data;
    } else {
      Message.error(message);
      return Promise.reject(new Error(message));
    }
  },
 async error => {
    if(error.response && error.response.data && error.response.data.code==10002){
        await store.dispatch('user/logout')
        router.push('/login')
    }
    Message.error(error.message);
    return Promise.reject(error);
  }
);
// 导出axios实例
export default service;
